package org.mycub.server.parser;

import org.json.JSONException;
import org.json.JSONObject;
import org.mycub.server.data.models.FreeParking;
import org.mycub.server.data.models.PublicToilet;
import org.mycub.server.datainserter.PublicToiletsInserter;
import org.mycub.server.datainserter.factory.PublicToiletsInserterFactory;
import org.opengis.feature.simple.SimpleFeature;



public class CollectPublicToilet extends CollectItem{

	FreeParking parking;

	public CollectPublicToilet() {
		this.setFreeParking(new FreeParking());
	}

	public void collect(JSONObject json) {
		
		PublicToilet toilet = new PublicToilet();
		
		try {
			String id = json.get("cle").toString();
			String address = json.get("adresse").toString();
			String nom = "NC";
			if(json.has("nom")){
				nom = json.get("nom").toString();
			}
			String options = "aucune";
			if(json.has("options")){
				options = json.get("options").toString();
			}
			String typo = json.get("typologie").toString();
			String lon = json.get("x_long").toString();
			String lat = json.get("y_lat").toString();
			
			
			toilet.setId(id);
			toilet.setName(nom);
			toilet.setAddress(address);
			toilet.setOptions(options);
			toilet.setTypo(typo);
			toilet.setLongitude((lon.isEmpty())?0:Double.valueOf(lon));
			toilet.setLatitude((lat.isEmpty())?0:Double.valueOf(lat));
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		System.out.println("collected public toilet : " + toilet.getName() + 
				" id = " + toilet.getId());

		savePublicToilet(toilet);
	}
	
	public void savePublicToilet(PublicToilet toiletCollected){
		PublicToiletsInserter publicToiletInserter = new PublicToiletsInserterFactory().createPublicToiletsInserter();
		publicToiletInserter.save(toiletCollected);
	}

	public FreeParking getFreeParking() {
		return parking;
	}

	public void setFreeParking(FreeParking parking) {
		this.parking = parking;
	}

	@Override
	public void collect(SimpleFeature feature) {

	}	
}
